###REPLICATION CODE

##Paper: "Who Settles Disputes? Treaty Design and Trade Attitudes toward the Transatlantic Trade and Investment Partnership (TTIP)"
##Journal: International Organization
##Authors: Hyeonho Hahm, Thomas Koenig, Moritz Osnabruegge and Elena Frech
##Date: February 18, 2019

rm(list=ls())

#Load packages
library(foreign)
library("ggplot2")


#Theme for figures
theme_bw1 = function(base_size = 13, base_family = "") {
  theme_grey(base_size = base_size, base_family = base_family) %+replace%
    theme(
      axis.text.x =       element_text(size = base_size, colour = "black",  hjust = .5 , vjust=1),
      axis.text.y =       element_text(size = base_size , colour = "black", hjust = 0 , vjust=.5 ), # changes position of X axis text
      axis.ticks =        element_line(colour = "grey50"),
      axis.title.y =      element_text(size = base_size,angle=90,vjust=.01,hjust=.1),
      legend.position = "none"
    )
}


#####
#Main results
#####

#Binary Choice
ggfeat1 = list(coord_flip(ylim=c(-.30,.30)),  
geom_hline(yintercept=0,size=.5,colour="darkgrey",linetype="dotted"),
geom_pointrange(mapping=aes(y=g$pe,x=g$var,ymin=g$lower,ymax=g$upper),size=.5),
scale_y_continuous(name="Change in Pr(Trade Agreement preferred)",breaks=round(seq(-.3,.3,.1),0.5),labels=c("-.3","-.2","-.1","0",".1",".2",".3")),
scale_x_discrete(name=""),
theme_bw1())

g = read.table("main_results.txt", sep=" ")
g = subset(g, subsetlabel=="Without weights")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
levels(g$var)

p = ggplot(data=g) + ggfeat1
pdf("main_results.pdf",width=7,height=8) 
print(p)
dev.off()


###Results by subgroups

ggfeat2 = list(
facet_grid(.~subsetlabel),
coord_flip(ylim=c(-.30,.30)),
geom_hline(yintercept=0,size=.5,colour="darkgrey",linetype="dotted"),
geom_pointrange(mapping=aes(y=g$pe,x=g$var,ymin=g$lower,ymax=g$upper),size=.5),
scale_y_continuous(name="Change in Pr(Trade Agreement preferred)",breaks=round(seq(-.3,.3,.1),0.5),labels=c("-.3","-.2","-.1","0",".1",".2",".3")),
scale_x_discrete(name=""),
theme_bw1()
)


#Figure 2
g = read.table("fig2.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
g$subsetlabel = factor(g$subsetlabel,levels=unique(g$subsetlabel))
g$color = "no"
g$color[as.character(g$subsetlabel)=="Heard about TTIP" | as.character(g$subsetlabel)=="Not heard about TTIP"] = "yes"

p = ggplot(data=g, mapping=aes(y=g$pe,x=g$var))
p = p + geom_rect(data=g,aes(fill=color),xmin=-Inf,xmax=Inf,ymin=-Inf,ymax=Inf,alpha=.1)
p = p + facet_grid(.~subsetlabel)
p = p + coord_flip(ylim=c(-.30,.20))
p = p + geom_hline(yintercept = 0,size=.5,colour="darkgrey",linetype="dotted") # lines to separate -1 and 1
p = p + geom_pointrange(mapping=aes(ymin=g$lower,ymax=g$upper),size=.5)
p = p + scale_y_continuous(name="Change in Pr(Trade Agreement preferred)",breaks=round(seq(-.3,.2,.1),0.5),labels=c("-.3","-.2", "-.1", "0", ".1", ".2"))
p = p + scale_x_discrete(name="")
p = p + scale_fill_manual(values=alpha(c("gray100","gray90"),0.1)) 
p = p + theme_bw1() 
p = p + theme(panel.background = element_rect(fill="black", colour="grey", size = 4))
pdf("fig2.pdf",width=16,height=8)
print(p)
dev.off()


#Figure A1: University education
g = read.table("uni_educ.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("uni_educ.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A2: Sector
g = read.table("sector.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
g$subsetlabel = factor(g$subsetlabel,levels=unique(g$subsetlabel))

p = ggplot(data=g) 
p = p + facet_grid(.~subsetlabel)
p = p + coord_flip(ylim=c(-.80,.40))
p = p + geom_hline(yintercept = 0,size=.5,colour="darkgrey",linetype="dotted") 
p = p + geom_pointrange(mapping=aes(y=g$pe,x=g$var,ymin=g$lower,ymax=g$upper),size=.5)
p = p + scale_y_continuous(name="Change in Pr(Trade Agreement preferred)",breaks=round(seq(-.8,.4,.2),0.5),labels=c("-.8", "-.6","-.4", "-.2","0",".2",".4"))
p = p + scale_x_discrete(name="")
p = p + theme_bw1()
pdf("sector.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A3: Opposition Bundestag
g = read.table("opp_bundestag.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("opp_bundestag.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A4: Center
g = read.table("center.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("center.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A5: EU Pro
g = read.table("eu_pro.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("eu_pro.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A6: Ideology
g = read.table("right.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
g$subsetlabel = factor(g$subsetlabel, levels=unique(g$subsetlabel))
p = ggplot(data=g) + ggfeat2
pdf("right.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A7: Weights
g = read.table("main_results.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("chosen.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A8: Round
g = read.table("round.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("round.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A9: Order
g = read.table("order.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat2
pdf("order.pdf",width=12,height=8)
print(p)
dev.off()


#Figure A10: Rating
ggfeat3 = list(coord_flip(ylim=c(-.30,.30)),  
geom_hline(yintercept=0,size=.5,colour="darkgrey",linetype="dotted"),
geom_pointrange(mapping=aes(y=g$pe,x=g$var,ymin=g$lower,ymax=g$upper),size=.5),
scale_y_continuous(name="Change in Pr(Trade Agreement favored)",breaks=round(seq(-.3,.3,.1),0.5),labels=c("-.3","-.2","-.1","0",".1",".2",".3")),
scale_x_discrete(name=""),
theme_bw1())

g = read.table("rating.txt", sep=" ")
g$var = factor(g$var,levels=unique(g$var)[length(g$var):1])
p = ggplot(data=g) + ggfeat3
pdf("rating.pdf",width=7,height=8)
print(p)
dev.off()